{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Linked List Nth to Last Node \n",
    "\n",
    "## Problem Statement\n",
    "Write a function that takes a head node and an integer value **n** and then returns the nth to last node in the linked list. For example, given:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Node:\n",
    "\n",
    "    def __init__(self, value):\n",
    "        self.value = value\n",
    "        self.nextnode  = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Example Input and Output:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = Node(1)\n",
    "b = Node(2)\n",
    "c = Node(3)\n",
    "d = Node(4)\n",
    "e = Node(5)\n",
    "\n",
    "a.nextnode = b\n",
    "b.nextnode = c\n",
    "c.nextnode = d\n",
    "d.nextnode = e\n",
    "\n",
    "# This would return the node d with a value of 4, because its the 2nd to last node.\n",
    "target_node = nth_to_last_node(2, a) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_node.value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solution\n",
    "Fill out your solution below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def nth_to_last_node(n, head):\n",
    "    slow, fast = head, head\n",
    "    while n > 0 and fast:\n",
    "        fast=fast.nextnode\n",
    "        n-=1\n",
    "    if not fast:\n",
    "        return head\n",
    "    while fast:\n",
    "        fast=fast.nextnode\n",
    "        slow=slow.nextnode\n",
    "    return slow\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test Your Solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ALL TEST CASES PASSED\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "RUN THIS CELL TO TEST YOUR SOLUTION AGAINST A TEST CASE \n",
    "\n",
    "PLEASE NOTE THIS IS JUST ONE CASE\n",
    "\"\"\"\n",
    "\n",
    "from nose.tools import assert_equal\n",
    "\n",
    "a = Node(1)\n",
    "b = Node(2)\n",
    "c = Node(3)\n",
    "d = Node(4)\n",
    "e = Node(5)\n",
    "\n",
    "a.nextnode = b\n",
    "b.nextnode = c\n",
    "c.nextnode = d\n",
    "d.nextnode = e\n",
    "\n",
    "####\n",
    "\n",
    "class TestNLast(object):\n",
    "    \n",
    "    def test(self,sol):\n",
    "        \n",
    "        assert_equal(sol(2,a),d)\n",
    "        print ('ALL TEST CASES PASSED')\n",
    "        \n",
    "# Run tests\n",
    "t = TestNLast()\n",
    "t.test(nth_to_last_node)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Good Job!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
